package com.sojson.task;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import com.sojson.config.condition.TaskCondition;
import com.sojson.constant.Constant;
import com.sojson.constant.Lock;
import com.sojson.enums.EOrderStatusHotel;
import com.sojson.enums.EPostApplyHireStatus;
import com.sojson.enums.EPostApplyPostsStatus;
import com.sojson.enums.EPostType;
import com.sojson.enums.core.EOrderStatus;
import com.sojson.project.dao.HotelOrderDao;
import com.sojson.project.dao.OrderDao;
import com.sojson.project.entity.vo.ActivityVo;
import com.sojson.project.entity.vo.CouponVo;
import com.sojson.project.entity.vo.HotelOrderVo;
import com.sojson.project.entity.vo.OrderVo;
import com.sojson.project.entity.vo.PostApplyVo;
import com.sojson.project.entity.vo.UserCouponVo;
import com.sojson.project.service.ActivityService;
import com.sojson.project.service.CouponService;
import com.sojson.project.service.HotelOrderService;
import com.sojson.project.service.OrderDepositService;
import com.sojson.project.service.PostApplyService;
import com.sojson.project.service.PostService;
import com.sojson.project.service.UserCouponService;
import com.sojson.util.DateUtil;
import com.sojson.util.file.FileUtil;
import com.sojson.util.page.PageUtil;
import com.sojson.util.poi.ExcelUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * Web定时任务
 * 
 * @author liu
 * @date 2021-04-28
 */
@Slf4j
@Component
/** 是否注入这个Bean */
@Conditional(TaskCondition.class)
public class CustomWebTask implements TaskInterface {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private CouponService couponService;
    @Autowired
    private UserCouponService userCouponService;
    @Autowired
    private OrderDepositService orderDepositService;
    @Autowired
    private HotelOrderService hotelOrderService;
    @Autowired
    private PostService postService;
    @Autowired
    private PostApplyService postApplyService;
    @Resource
    private OrderDao orderDao;
    @Resource
    private HotelOrderDao hotelOrderDao;

    /**
     * 清空本地导出的Excel文件
     */
    public void clearExportExcelThisLocal() {
        File file = new File(FileUtil.THIS_LOCALITY_DIRECTORY_BUCKET_NAME_BASE + ExcelUtil.FILE_PATH);
        if (!file.exists()) {
            return;
        }

        File[] listFiles = file.listFiles();
        for (File f : listFiles) {
            f.delete();
        }
        log.info("---------------当天0 点 清除本地导出的Excel文件----------------");
    }

    /**
     * 关闭已结束的活动和优惠券
     */
    public void realTimeSale() {
        Date nowDate = DateUtil.nowDate();

        ActivityVo activity = new ActivityVo();
        activity.setThisTime(nowDate);
        activityService.updateStatusToEnd(activity);
        log.info("---------------当天0 点 关闭已结束的活动----------------");

        CouponVo coupon = new CouponVo();
        coupon.setThisTime(nowDate);
        couponService.updateStatusToEnd(coupon);
        UserCouponVo userCoupon = new UserCouponVo();
        userCoupon.setThisTime(nowDate);
        userCouponService.updateStatusToEnd(userCoupon);
        log.info("---------------当天0 点 关闭已结束的优惠券----------------");
    }

    /**
     * 签收已发货超过15天未签收的订单
     */
    public void orderSignFor() {
        // 查出所有订单
        Date nowDate = DateUtil.nowDate();
        nowDate.setTime(nowDate.getTime() - Constant.ORDER_SIGN_FOR_TIME);

        OrderVo orderVo = new OrderVo();
        orderVo.setStatus(EOrderStatus.SEND_OUT_YES.getCode());
        orderVo.setSendTimeOut(nowDate);
        log.info("---------------当天0 点 自动签收已发货超过15天未签收的订单----------------");
    }

    /**
     * 取消已超时未支付的订单
     */
    public void orderTime() {
        OrderVo orderVo = new OrderVo();
        orderVo.setStatus(EOrderStatus.PAYMENT_NO.getCode());
        Date nowDate = DateUtil.nowDate();
        nowDate.setTime(nowDate.getTime() - Constant.ORDER_PAY_TIME - Constant.MILLIS_MINUTE_THIRTY);
        orderVo.setOverTime(nowDate);
        log.info("---------------当天0 点 取消已超时未支付的订单----------------");
    }

    /**
     * 取消已超时未支付的诚意金订单
     */
    public void orderDepositTime() {
        Date nowDate = new Date(DateUtil.nowLong() - Constant.ORDER_DEPOSIT_PAY_TIME - Constant.MILLIS_MINUTE_THIRTY);

        synchronized (Lock.ORDER_STATUS) {
            orderDepositService.cancelAuto(nowDate);
        }
        log.info("---------------当天0 点 取消已超时未支付的诚意金订单----------------");
    }

    /**
     * 取消已超时未支付的酒店订单
     */
    public void orderHotelTime() {
        HotelOrderVo orderVo = new HotelOrderVo();
        orderVo.setStatus(EOrderStatusHotel.PAYMENT_NO.getCode());
        Date nowDate = DateUtil.nowDate();
        nowDate.setTime(nowDate.getTime() - Constant.ORDER_PAY_TIME_HOTEL - Constant.MILLIS_MINUTE_THIRTY);
        orderVo.setOverTime(nowDate);
        log.info("---------------当天0 点 取消已超时未支付的酒店订单----------------");
    }

    /**
     * 修改待入住的状态为待评论
     */
    public void orderHotelToCommentNo() {
        // 查出所有订单
        Date nowDate = DateUtil.nowDate();
        nowDate.setTime(nowDate.getTime() - Constant.ORDER_HOTEL_SIGN_FOR_TIME);
        List<Integer> arrayList = new ArrayList<>();
        arrayList.add(EOrderStatusHotel.CHECK_NO.getCode());
        arrayList.add(EOrderStatusHotel.CHECK_YES.getCode());

        HotelOrderVo order = new HotelOrderVo();
        order.setStatuss(arrayList);
        order.setTimeOut(nowDate);
        List<String> findNosByVo = hotelOrderService.findNosByVo(order);

        // 修改订单状态
        for (String no : findNosByVo) {
            try {
                hotelOrderService.updateStatusToCommentNo(no);
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                log.error("修改待入住的状态为待评论失败，" + e.getMessage());
            }
        }
        log.info("---------------当天0 点 修改待入住的状态为待评论----------------");
    }

    /**
     * 关闭上岗时间已到的职位
     */
    public void closePost() {
        postService.closePost(DateUtil.nowDate());
        log.info("---------------当天0 点 关闭上岗时间已到的职位----------------");
    }

    /**
     * 超过结束时间一天没有录用的职位自动改为未录用
     */
    public void hireNo() {
        PostApplyVo postApply = new PostApplyVo();
        postApply.setHireStatus(EPostApplyHireStatus.HIRE_ING.getCode());
        postApply.setPostType(EPostType.PART_TIME.getCode());
        postApply.setCloseTimeReady(new Date(DateUtil.nowLong() - Constant.MILLIS_DAY));
        log.info("---------------当月1号0点 超过结束时间一天没有录用的职位自动改为未录用----------------");
    }

    /**
     * 超过结束时间一天未上岗的职位自动改为未上岗
     */
    public void postsNo() {
        PostApplyVo postApply = new PostApplyVo();
        postApply.setPostsStatus(EPostApplyPostsStatus.AUDIT.getCode());
        postApply.setPostType(EPostType.PART_TIME.getCode());
        postApply.setCloseTimeReady(new Date(DateUtil.nowLong() - Constant.MILLIS_DAY));
        PageUtil.all(postApply);

        List<PostApplyVo> list = postApplyService.list(postApply);
        for (PostApplyVo postApplyVo : list) {
            try {
                postApplyService.postsNoWeb(postApplyVo);
            } catch (Exception e) {
            }
        }
        log.info("---------------当天0 点 超过结束时间一天未上岗的职位自动改为未上岗----------------");
    }

}